<?php

/**
 * 鸡蛋白活动页面
 * 编写人   陈泽富
*/

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . '/includes/funGlair.php');
$act = !empty($_REQUEST['act'])?$_REQUEST['act']:'';

/*打印缓存数据*/
if ($act=="dump") {
    dump('已抢购队列：' . Q()->get("queueGlair{$version}"));
    dump('已处理队列：' . Q()->get("queueGlair2{$version}"));
    dump('已参与领取人数：' . C()->getCounter("counter{$version}"));
    dump('当前库存：' . C()->getCounter("glairStock{$version}"));
    exit;
}

/*领取鸡蛋白*/
if($act =='get_glair'){
    $return = array('error'=>0,'message'=>'','content'=>'');
    $return = get_glair();
    die(JSON()->encode($return, false));
}

/*领取鸡蛋白包邮券*/
if($act =='get_glairBaoyou'){
    $return = array('error'=>0,'message'=>'','content'=>'');
    $return = get_glairBaoyou();
    die(JSON()->encode($return, false));
}

/*兑换鸡蛋白*/
if($act =='exchange'){
    $return=array('error'=>0,'message'=>'','content'=>'');
    $code = !empty($_REQUEST['code'])?$_REQUEST['code']:'0';
    $return = exchangeGlair($code);
    die(JSON()->encode($return));
}

//提交评论
if($act =='comment'){
        require(ROOT_PATH . 'includes/cls_json.php');
        $json   = new JSON;
        $user_id = !empty($_SESSION['user_id'])?$_SESSION['user_id']:'';
        $comment = !empty($_REQUEST['comment'])?$_REQUEST['comment']:'';
        $time=time();
        $return['error']=1;
        $return['content']="";

        //查询用户是否评论
        $sql = "SELECT comment_id FROM ". $ecs->table('comment_white') ."WHERE user_id = $user_id ";
        $comment_id = $db->getOne($sql);
        if($comment_id){
            $return['error']=2;
            $return['content']='每人只能评论一次';
            echo $json->encode($return);
            exit();
        }
        /*查询用户名与头像*/
        $sql =  "SELECT u.user_name,ui.head_url FROM ".$ecs->table('users')."AS u".
                " LEFT JOIN ".$ecs->table('user_info')."AS ui ON ui.user_id = u.user_id ".
                " WHERE u.user_id = '".$user_id."'";
        $user_info = $db->getRow($sql);
        $sql = 'INSERT INTO ' . $ecs->table('comment_white') . ' ( comment_content,add_time,head_url,user_name) ' .
               "VALUES('$comment','$time','".$user_info['head_url']."','".$user_info['user_name']."')";
        $db->query($sql);
        if($db->insert_id()){
            $return['content']='评论成功';
            echo $json->encode($return);
            exit();
        }
        $return['error']=2;
        $return['content']='每人只能评论一次';
        echo $json->encode($return);
        exit();
}
//点赞
if($act =='agree'){
        require(ROOT_PATH . 'includes/cls_json.php');
        $json   = new JSON;
        $return['error']=1;
        $return['content']="";
        $comment_id = !empty($_REQUEST['comment_id'])?$_REQUEST['comment_id']:'';
        if(empty($comment_id)||!empty($_SESSION['agree_num'])){
            $return['error']=2;
            $return['content']="请勿重复点赞";
            echo $json->encode($return);
            exit();
        }
        $sql="UPDATE ".$GLOBALS['ecs']->table('comment_white').
             " set agree_num=(agree_num+1)".
             " WHERE comment_id=$comment_id";
        $db->query($sql);
        $return['content']="点赞成功";
        $_SESSION['agree_num']=1;
        echo $json->encode($return);
        exit();
}
//获取更多评论信息
if($act =='more_comment'){
    require(ROOT_PATH . 'includes/cls_json.php');
    $json   = new JSON;
    $return['error']=1;
    $pages = !empty($_REQUEST['pages'])?$_REQUEST['pages']:'1';
    $size = "10";
    $start = ($pages - 1) * $size + 3;

    $return['content']="";
    //获取评论
    $sql =" SELECT c.comment_content ,c.comment_id , c.add_time, c.agree_num ,c.head_url,c.user_name FROM ".
          $ecs->table('comment_white') ." AS c ".
          " ORDER BY c.add_time ASC".
          " LIMIT $start , $size";
    $comment_list =$db->getall($sql);
    foreach ($comment_list as $key => $value) {
        $comment_list[$key]['add_time'] = date("Y-m-d H:i:s",$value['add_time']);
    }
    $smarty->assign('comment_list',           $comment_list);
    if(!empty($comment_list)){
        $return['content'] = $smarty->fetch("library/comment_white.lbi");
    }
    else{
        $return['error']=2;
        $return['content'] = "已加载全部评论";
    }
    echo $json->encode($return);
    exit();
}

/* 显示模板页面 */
/* ----------------------------------------------------------------------- */
// if (empty($_REQUEST['isInfo'])) {
//     header('location: glairPoject.php');
//     exit;
// }

assign_template();
$smarty->assign('page_title',      '鸡蛋美术商城-鸡蛋白'); // 页面标题
$smarty->assign('keywords',        '鸡蛋白,' . htmlspecialchars($_CFG['shop_keywords'])); // SEO网站关键词
$smarty->assign('description',     htmlspecialchars($_CFG['shop_desc'])); // SEO网站描述

$time = time();
/* 模板文案 */
if ($time < strtotime('2015-09-19')) {
    $smarty->assign('pici', '第一批');
    $smarty->assign('startDate', date('m月d日', strtotime('2015-09-18')));
    $smarty->assign('startDate2', date('Y年m月d日', strtotime('2015-09-19')));
} elseif ($time < strtotime('2015-09-20')) {
    $smarty->assign('pici', '第二批');
    $smarty->assign('startDate', date('m月d日', strtotime('2015-09-19')));
    $smarty->assign('startDate2', date('Y年m月d日', strtotime('2015-09-20')));
} elseif ($time < strtotime('2015-09-21')) {
    $smarty->assign('pici', '第三批');
    $smarty->assign('startDate', date('m月d日', strtotime('2015-09-20')));
    $smarty->assign('startDate2', date('Y年m月d日', strtotime('2015-09-21')));
} elseif ($time < strtotime('2015-09-22')) {
    $smarty->assign('pici', '第四批');
    $smarty->assign('startDate', date('m月d日', strtotime('2015-09-21')));
    $smarty->assign('startDate2', date('Y年m月d日', strtotime('2015-09-22')));
} elseif ($time >= strtotime('2015-09-22')) {
    $smarty->assign('pici', '第五批');
    $smarty->assign('startDate', date('m月d日', strtotime('2015-09-22')));
    $smarty->assign('startDate2', date('Y年m月d日', strtotime('2015-09-23')));
}


//获取评论
$sql =" SELECT c.comment_content ,c.comment_id , c.add_time, c.agree_num ,c.head_url,c.user_name FROM ".
      $ecs->table('comment_white') ." AS c ".
      " ORDER BY c.add_time ASC".
      " LIMIT 0 , 3";
$comment_list= $db->getall($sql);

foreach ($comment_list as $key => $value) {
    $comment_list[$key]['add_time'] = date("Y-m-d H:i:s",$value['add_time']);
}
//获取评论总数
$sql = " SELECT count(comment_id) FROM ".$ecs->table('comment_white') ;
$comment_count = $db->getOne($sql);

$comment_count +=126;
//获取用户是否显示分享领取按钮
$user_id = !empty($_SESSION['user_id'])?$_SESSION['user_id']:'';
$smarty->assign('comment_count',           $comment_count);
$smarty->assign('comment_list',           $comment_list);

$smarty->assign('message',           $message);
$smarty->display('glair/glair_fans.dwt');

